package com.xhs.zdl.demo.tools;

import android.app.Activity;
import android.content.Context;
import android.view.WindowManager;

/**
 * @author zdl
 * @date 2017/12/28 13:54
 * email zdl328465042@163.com
 * explain 手机相关参数的工具类
 */

public class MyPhoneUtils {

    /**
     * 根据手机的分辨率从dp的单位转成为px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从px(像素)的单位转成为dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     *            屏幕透明度0.0-1.0 1表示完全不透明
     */
    public static void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams layoutParams = activity.getWindow()
                .getAttributes();
        layoutParams.alpha = bgAlpha;
        activity.getWindow().setAttributes(layoutParams);
    }
}
